package com.example.foreapp;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class FavoriteActivity extends ListActivity{
	
	private DbOpenHelper db;
	private PackageManager pm;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		pm=this.getPackageManager();
		db=DbUtil.getIns(this);
		setTitle("收藏");
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		createView();
	}
	
	private void createView(){
		Cursor cur=db.query("time", false);
		List<ActivityInfo> apps=new ArrayList<ActivityInfo>();
		ActivityInfo info=null;
		while(cur.moveToNext()){
			String favor=cur.getString(cur.getColumnIndex("favor"));
			if("t".equals(favor)){
				info=new ActivityInfo();
				info.setTime(cur.getString(cur.getColumnIndex("time")));
				info.setLabel(cur.getString(cur.getColumnIndex("applabel")));
				info.setClassName(cur.getString(cur.getColumnIndex("classname")));
				info.setPkgName(cur.getString(cur.getColumnIndex("packagename")));
				apps.add(info);
			}
		}
		cur.close();
		setListAdapter(new FavoriteAdapter(apps));
		getListView().setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				ActivityInfo info=(ActivityInfo)parent.getItemAtPosition(position);
				Intent intent=new Intent();
				intent.setPackage(info.getPkgName());
				intent.setClassName(info.getPkgName(), info.getClassName());
				try {
					startActivity(intent);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					Toast.makeText(getApplicationContext(), info.getClassName()+"该Activity不存在", Toast.LENGTH_SHORT).show();
				}
			}
			
		});
	}
	
	
	
	class FavoriteAdapter extends BaseAdapter{

		private List<ActivityInfo> apps;
		
		public FavoriteAdapter(List<ActivityInfo> apps) {
			super();
			this.apps = apps;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return apps.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return apps.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View view = null;
			Holder holder = null;
			if (convertView == null || convertView.getTag() == null) {
				view = LayoutInflater.from(getApplicationContext()).inflate(
						R.layout.app_list_item, null);
				holder = new Holder();
				holder.appicon = (ImageView) view.findViewById(R.id.appicon);
				holder.label = (TextView) view.findViewById(R.id.appname);
				holder.packagename = (TextView) view
						.findViewById(R.id.pathname);
				view.setTag(holder);
			} else {
				view = convertView;
				holder = (Holder) convertView.getTag();
			}
			ActivityInfo info = (ActivityInfo)getItem(position);
			Intent aIntent = new Intent(Intent.ACTION_MAIN, null);
			aIntent.setPackage(info.getPkgName());
			List<ResolveInfo> resolveInfos = pm.queryIntentActivities(aIntent,
					PackageManager.MATCH_DEFAULT_ONLY);
			Collections.sort(resolveInfos,
					new ResolveInfo.DisplayNameComparator(pm));
			if (resolveInfos.size() > 0) {
				holder.appicon.setImageDrawable(resolveInfos.get(0)
						.loadIcon(pm));
			}
			holder.label.setText(info.getLabel());
			holder.packagename.setText(info.getPkgName());
			return view;
		}

	}

	static class Holder {
		ImageView appicon;
		TextView label;
		TextView packagename;

	}
}
